/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package inference;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jess.*;
import jess.awt.TextAreaWriter;

/**
 *
 * @author TOSHIBA
 */
public class Solver {
   /*public static void main(String[] unused) {
    try {
        Rete r = new Rete();
       
        r.eval("(deftemplate person (slot name) (slot age))");
        r.eval("(assert (person (name gio) (age 20)))");
        r.eval("(assert (person (name robert) (age 21)))");
        r.eval("(defrule kodok (person {age > 20}) => (printout t \"umur dua satu\"))");
        r.eval("(defrule kodok2 (person {age < 99}) => (printout t \"testing\"))");
        Value v = r.eval("(run)");

        // Prints '9'
        System.out.println(v.stringValue(r.getGlobalContext()));
    } catch (JessException ex) {
        System.err.println(ex);
    }
  }*/
    
    //public  ArrayList<Fact> facts = new ArrayList<Fact>();
    public  Rete core;
    public  String age; 
    public  String gender; 
    public  String hobby; 
    public  String status; 
    public  String spareTime;
    public  String budget;
    public String[] result = new String[100];
    public String resulttest;
    
    public Solver(){
         core = new Rete();
    }
    
    public  void addFactsToTemplate(){
        try {
            Fact p = new Fact("person", core);
            p.setSlotValue("gender", new Value(gender, RU.STRING));
            p.setSlotValue("age", new Value(age, RU.STRING));
            p.setSlotValue("status", new Value(status, RU.STRING));
            
            Fact a = new Fact("activity", core);
            a.setSlotValue("spareTime", new Value(spareTime, RU.STRING));
            a.setSlotValue("hobby", new Value(hobby, RU.STRING));
            
            Fact s = new Fact("present", core);
            s.setSlotValue("budget", new Value(budget, RU.STRING));
            
            core.assertFact(p);
            core.assertFact(a);
            core.assertFact(s);
            
        } catch (JessException ex) {
            Logger.getLogger(Solver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void batch(String filepath){
        try {
            core.batch(filepath);
        } catch (JessException ex) {
            Logger.getLogger(Solver.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void exec(){ 
        try {
            //assert fakta
            this.addFactsToTemplate();
//             System.out.println(this.core.findFactByID(0).toString());
//            System.out.println(this.core.findFactByID(1).toString());
//             System.out.println(this.core.findFactByID(2).toString());
//             System.out.println(this.core.findFactByID(3).toString());
//             System.out.println(this.core.findFactByID(4).toString());
//             System.out.println(this.core.findFactByID(5).toString());
            //jalankan
            Value resultval = core.eval("(run)");
            resulttest = resultval.stringValue(core.getGlobalContext());
            
            
            //split dulu stringnya
            //result = resultStr.split(";");
    
            
            //cek, ada yg sama atau nggak
            //kirim hasilnya (array of string) ke GUI
            
            
        } catch (JessException ex) {
            Logger.getLogger(Solver.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public List getUniqueElements(List l){
        List l2 = new ArrayList();
        
        for(int i=0; i<l.size(); i++){
            if (!l2.contains(l.get(i))){ 
                l2.add(l.get(i));
            }
        }
        return l2;
    }
    
    public  void main(String[] unused) {
//        try {
//            Rete r = new Rete();
//            
//
//            r.eval("(deftemplate person (slot name) (slot age))");
//            r.eval("(defrule kodok (person {age > 20}) => (printout t \"umur dua satu\"))");
//            r.eval("(defrule kodok2 (person {age < 99}) => (printout t \" kurang dari 99\"))");
//
////        r.assertFact();
//
////        r.batch("");
////        r.reset();
////        r.run();
//
//            Value v = r.eval("(run)");
//
//            // Prints '9'
//            System.out.println(v.stringValue(r.getGlobalContext()));
//        } catch (JessException ex) {
//            System.err.println(ex);
//        }
    }
}
